<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-1
 * Time: 下午6:43
 * To change this template use File | Settings | File Templates.
 */
class register extends MY_Controller
{
    public function index()
    {
        $redirectUrl = $this->input->get_post('redirect_url');
        $source = $this->input->get_post('source');

        $vData = array(
            'title' => '新用户注册',
            'redirect_url' => $redirectUrl,
            'source' => $source,
            'keywords' => '电影1010 注册',
            'description' => '电影1010 全网最全资源库',
        );
        $this->load->view('register', $vData);
    }

    public function submit()
    {
        $this->load->helper('validation');

        $userName = $this->input->get_post('username');
        $passWord = $this->input->get_post('password');
        $verifyCode = $this->input->get_post('verify_code');
        $source = $this->input->get_post('source');
        $redirectUrl = $this->input->get_post('redirect_url');
        $redirect_url = is_url($redirectUrl) ? $redirectUrl : config_item('i_base_url').'login/index';

        $response = array('error' => '0', 'msg' => '注册用户成功', 'code' => 'register_user_success');
        $response['redirect_url'] = $redirect_url;

        do {
            if (empty ($userName) || empty ($passWord)) {
                $response = array('error' => '1001', 'msg' => '用户名或密码为空', 'code' => 'username_and_password_empty');
                break;
            }

            if (!length_limit($userName, 6, 32)) {
                $response = array('error' => '1002', 'msg' => '用户名格式错误', 'code' => 'user_format_error');
                break;
            }

            if (!length_limit($passWord, 6, 32)) {
                $response = array('error' => '1003', 'msg' => '密码格式错误', 'code' => 'password_format_error');
                break;
            }

            /* 此处写验证码 */
            if ('' === $verifyCode || md5(strtolower($verifyCode)) !== ($this->getVerifyCode())) {
                $response = array('error' => '10005', 'msg' => '验证码错误', 'code' => 'verification_code_error');
                break;
            }

            $this->usetVerifyCode();

            $info = array(
                'username' => $userName,
                'password' => $passWord,
                'email' => '',
                'source' => $source,
            );
            $this->load->model('user');
            $uId = $this->user->save($info);

            if (!$uId) {
                $response = array('error' => '1004', 'msg' => '注册用户失败', 'code' => 'register_failure');
                break;
            }
        } while (false);

        self::json_output($response);
    }

    /**
     * 检查用户名是否存在
     */
    public function checkUserName()
    {
        $username = $this->input->get_post('username');

        $this->load->helper('validation');

        $response = array('error' => '10035', 'msg' => '用户可以使用', 'code' => 'you_can_use_the');

        if (!length_limit($username, 6, 32)) {
            $response = array('error' => '10001', 'msg' => '用户名不合法', 'code' => 'username_illegal');
        }
        else
        {
            $this->load->model('user');
            if ($this->user->userNameIsExist($username)) {
                $response = array('error' => '10002', 'msg' => '用户已存在', 'code' => 'user_already_exists');
            }
        }

        echo self::json_output($response);
    }

    /**
     * 显示验证码图片
     */
    public function verifyCode()
    {
        $code = $this->setVerifyCode();
        //$this->input->set_cookie('verify_code', $code, 60);
        $this->lib('captcha', array('code'=>$code));
        $this->captcha->display();
    }

    public function checkVerifyCode()
    {
        $verifyCode = $this->input->get_post('verify_code');

        //$vc = md5(strtolower($verifyCode));

        if ('' === $verifyCode || md5(strtolower($verifyCode)) !== ($this->getVerifyCode())) {
            $response = array('error' => '10005', 'msg' => '验证码错误', 'code' => 'verification_code_error');
        } else {
            $response = array('error' => '0', 'msg' => '验证成功', 'code' => 'verify_success');
        }

        self::json_output($response, true);
    }

    public function show()
    {
        echo $this->getVerifyCode();
    }

    /**
     * 生成验证码
     * @param int $lenght
     * @return mixed
     */
    private function setVerifyCode($lenght = 4)
    {

        $this->load->helper('string');
        $rand_str =  str_replace(array('0','O','1','l'), array('o','o','L','L'), random_string('alnum', $lenght));
        $newdata = array('verifyCode'  => md5(strtolower($rand_str)));
        $this->lib('session');
        $this->session->set_userdata($newdata);
        return $rand_str;
    }

    /**
     * 获取验证码
     * @return mixed
     */
    private function getVerifyCode()
    {
        $this->lib('session');
        return $this->session->userdata('verifyCode');
    }

    /**
     * 销毁验证码
     * @return mixed
     */
    private function usetVerifyCode()
    {
        $this->lib('session');
        $this->session->unset_userdata('verifyCode');
        return;
    }
}
